package cz.myslivec.slovicka.entity;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity
@Table(name="VOCABLE")
public class Vocable implements Serializable {
	private static final long serialVersionUID = 4708994778627720961L;

	@Id
	@Column(name = "ID_VOC")
	@GeneratedValue(strategy=GenerationType.AUTO)
	private Long id;

	@ManyToMany(mappedBy="vocables", fetch=FetchType.EAGER)
	private List<Lesson> lessons;
	
	@Column(nullable=false)
	private String str1;
	
	@Column(nullable=false)
	private String str2;
	
	private String str3;
	
	private String str4;
	
	@Temporal(TemporalType.TIMESTAMP)
	@Column(name="NEXT_TEST_DATE")
	private Date nextTestDate;
	
	@Column(name="E_FACTOR", nullable=false)
	private BigDecimal eFactor = new BigDecimal(2.5);
	
	@Column(name="TEST_COUNT", nullable=false)
	private Integer testCount = new Integer(1);

	@Column(name="TEST_INTERVAL", nullable=false)
	private Integer testInterval = new Integer(1);
	
	public Vocable() {
		super();
	}
	
	/**
	 * method for displaying list of lessons in string
	 * @return
	 */
	public String lessonsToStr() {
		if(lessons == null || lessons.isEmpty()) {
			return "";
		}
		StringBuilder sb = new StringBuilder();
		int i = 0;
		for(Lesson lesson : lessons) {
			if(i > 0) {
				sb.append(", ");
			} 
			sb.append(lesson.getId());
			i++;
		}
		return sb.toString();
	}
	
	public List<Lesson> getLessons() {
		return lessons;
	}

	public void setLessons(List<Lesson> lessons) {
		this.lessons = lessons;
	}

	public String getStr1() {
		return str1;
	}

	public void setStr1(String str1) {
		this.str1 = str1;
	}

	public String getStr2() {
		return str2;
	}

	public void setStr2(String str2) {
		this.str2 = str2;
	}

	public String getStr3() {
		return str3;
	}

	public void setStr3(String str3) {
		this.str3 = str3;
	}

	public String getStr4() {
		return str4;
	}

	public void setStr4(String str4) {
		this.str4 = str4;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Date getNextTestDate() {
		return nextTestDate;
	}

	public void setNextTestDate(Date nextTestDate) {
		this.nextTestDate = nextTestDate;
	}

	public BigDecimal geteFactor() {
		return eFactor;
	}

	public void seteFactor(BigDecimal eFactor) {
		this.eFactor = eFactor;
	}

	public Integer getTestCount() {
		return testCount;
	}

	public void setTestCount(Integer testCount) {
		this.testCount = testCount;
	}

	public Integer getTestInterval() {
		return testInterval;
	}

	public void setTestInterval(Integer testInterval) {
		this.testInterval = testInterval;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Vocable other = (Vocable) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "Vocable [id=" + id + ", lessons=" + lessons + ", str1=" + str1
				+ ", str2=" + str2 + ", str3=" + str3 + ", str4=" + str4
				+ ", nextTestDate=" + nextTestDate + ", eFactor=" + eFactor
				+ ", testInterval=" + testInterval
				+ ", testCount=" + testCount + "]";
	}

}
